home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / bison / byacc.zoo / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-01-23  |  6.7 KB  |  388 lines

  1. #include <signal.h>
  2. #include "defs.h"
  3.  
  4. char dflag;
  5. char lflag;
  6. char rflag;
  7. char tflag;
  8. char vflag;
  9.  
  10. char *file_prefix = "y";
  11. char *myname = "yacc";
  12. #ifdef atarist
  13. char *temp_form = "yXXXXXXX";
  14. #else
  15. char *temp_form = "yacc.XXXXXXX";
  16. #endif
  17.  
  18. int lineno;
  19. int outline;
  20.  
  21. char *action_file_name;
  22. char *code_file_name;
  23. char *defines_file_name;
  24. char *input_file_name = "";
  25. char *output_file_name;
  26. char *text_file_name;
  27. char *union_file_name;
  28. char *verbose_file_name;
  29.  
  30. FILE *action_file;    /*  a temp file, used to save actions associated    */
  31.             /*  with rules until the parser is written        */
  32. FILE *code_file;    /*  y.code.c (used when the -r option is specified) */
  33. FILE *defines_file;    /*  y.tab.h                        */
  34. FILE *input_file;    /*  the input file                    */
  35. FILE *output_file;    /*  y.tab.c                        */
  36. FILE *text_file;    /*  a temp file, used to save text until all        */
  37.             /*  symbols have been defined                */
  38. FILE *union_file;    /*  a temp file, used to save the union            */
  39.             /*  definition until all symbol have been        */
  40.             /*  defined                        */
  41. FILE *verbose_file;    /*  y.output                        */
  42.  
  43. int nitems;
  44. int nrules;
  45. int nsyms;
  46. int ntokens;
  47. int nvars;
  48.  
  49. int   start_symbol;
  50. char  **symbol_name;
  51. short *symbol_value;
  52. short *symbol_prec;
  53. char  *symbol_assoc;
  54.  
  55. short *ritem;
  56. short *rlhs;
  57. short *rrhs;
  58. short *rprec;
  59. char  *rassoc;
  60. short **derives;
  61. char *nullable;
  62.  
  63. extern char *mktemp();
  64. extern char *getenv();
  65.  
  66.  
  67. done(k)
  68. int k;
  69. {
  70.     if (action_file) { fclose(action_file); unlink(action_file_name); }
  71.     if (text_file) { fclose(text_file); unlink(text_file_name); }
  72.     if (union_file) { fclose(union_file); unlink(union_file_name); }
  73.     exit(k);
  74. }
  75.  
  76.  
  77. onintr()
  78. {
  79.     done(1);
  80. }
  81.  
  82.  
  83. set_signals()
  84. {
  85. #ifdef SIGINT
  86.     if (signal(SIGINT, SIG_IGN) != SIG_IGN)
  87.     signal(SIGINT, onintr);
  88. #endif
  89. #ifdef SIGTERM
  90.     if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
  91.     signal(SIGTERM, onintr);
  92. #endif
  93. #ifdef SIGHUP
  94.     if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
  95.     signal(SIGHUP, onintr);
  96. #endif
  97. }
  98.  
  99.  
  100. usage()
  101. {
  102.     fprintf(stderr, "usage: %s [-dlrtv] [-b file_prefix] filename\n", myname);
  103.     exit(1);
  104. }
  105.  
  106.  
  107. getargs(argc, argv)
  108. int argc;
  109. char *argv[];
  110. {
  111.     register int i;
  112.     register char *s;
  113.  
  114.     if (argc > 0) myname = argv[0];
  115.     for (i = 1; i < argc; ++i)
  116.     {
  117.     s = argv[i];
  118.     if (*s != '-') break;
  119.     switch (*++s)
  120.     {
  121.     case '\0':
  122.         input_file = stdin;
  123.         if (i + 1 < argc) usage();
  124.         return;
  125.  
  126.     case '-':
  127.         ++i;
  128.         goto no_more_options;
  129.  
  130.     case 'b':
  131.         if (*++s)
  132.          file_prefix = s;
  133.         else if (++i < argc)
  134.         file_prefix = argv[i];
  135.         else
  136.         usage();
  137.         continue;
  138.  
  139.     case 'd':
  140.         dflag = 1;
  141.         break;
  142.  
  143.     case 'l':
  144.         lflag = 1;
  145.         break;
  146.  
  147.     case 'r':
  148.         rflag = 1;
  149.         break;
  150.  
  151.     case 't':
  152.         tflag = 1;
  153.         break;
  154.  
  155.     case 'v':
  156.         vflag = 1;
  157.         break;
  158.  
  159.     default:
  160.         usage();
  161.     }
  162.  
  163.     for (;;)
  164.     {
  165.         switch (*++s)
  166.         {
  167.         case '\0':
  168.         goto end_of_option;
  169.  
  170.         case 'd':
  171.         dflag = 1;
  172.         break;
  173.  
  174.         case 'l':
  175.         lflag = 1;
  176.         break;
  177.  
  178.         case 'r':
  179.         rflag = 1;
  180.         break;
  181.  
  182.         case 't':
  183.         tflag = 1;
  184.         break;
  185.  
  186.         case 'v':
  187.         vflag = 1;
  188.         break;
  189.  
  190.         default:
  191.         usage();
  192.         }
  193.     }
  194. end_of_option:;
  195.     }
  196.  
  197. no_more_options:;
  198.     if (i + 1 != argc) usage();
  199.     input_file_name = argv[i];
  200. }
  201.  
  202.  
  203. char *
  204. allocate(n)
  205. unsigned n;
  206. {
  207.     register char *p;
  208.  
  209.     p = NULL;
  210.     if (n)
  211.     {
  212.     p = CALLOC(1, n);
  213.     if (!p) no_space();
  214.     }
  215.     return (p);
  216. }
  217.  
  218.  
  219. create_file_names()
  220. {
  221.     int i, len;
  222.  
  223. #ifdef atarist
  224.     char tmpdir[128], *tgetenv;
  225.     tgetenv = getenv("TEMP");    /* same as Gcc */
  226.     if (tgetenv == 0) 
  227.     strcpy(tmpdir, "./"); /* note: gcc lib translates '/' to '\' */
  228.     else
  229.     {
  230.     strcpy(tmpdir, tgetenv);
  231.     strcat(tmpdir, "/");
  232.     }
  233. #else
  234.     char *tmpdir;
  235.     tmpdir = getenv("TMPDIR");
  236.     if (tmpdir == 0) tmpdir = "/tmp";
  237. #endif
  238.  
  239.     len = strlen(tmpdir);
  240.     i = len + 13;
  241.     if (len && tmpdir[len-1] != '/')
  242.     ++i;
  243.  
  244.     action_file_name = MALLOC(i);
  245.     if (action_file_name == 0) no_space();
  246.     text_file_name = MALLOC(i);
  247.     if (text_file_name == 0) no_space();
  248.     union_file_name = MALLOC(i);
  249.     if (union_file_name == 0) no_space();
  250.  
  251.     strcpy(action_file_name, tmpdir);
  252.     strcpy(text_file_name, tmpdir);
  253.     strcpy(union_file_name, tmpdir);
  254.  
  255.     if (len && tmpdir[len - 1] != '/')
  256.     {
  257.     action_file_name[len] = '/';
  258.     text_file_name[len] = '/';
  259.     union_file_name[len] = '/';
  260.     ++len;
  261.     }
  262.  
  263.     strcpy(action_file_name + len, temp_form);
  264.     strcpy(text_file_name + len, temp_form);
  265.     strcpy(union_file_name + len, temp_form);
  266.  
  267. #ifdef atarist
  268.     action_file_name[len + 2] = 'a';
  269.     text_file_name[len + 2] = 't';
  270.     union_file_name[len + 2] = 'u';
  271. #else
  272.     action_file_name[len + 5] = 'a';
  273.     text_file_name[len + 5] = 't';
  274.     union_file_name[len + 5] = 'u';
  275. #endif
  276.  
  277.     mktemp(action_file_name);
  278.     mktemp(text_file_name);
  279.     mktemp(union_file_name);
  280.  
  281.     len = strlen(file_prefix);
  282.  
  283.     output_file_name = MALLOC(len + 7);
  284.     if (output_file_name == 0)
  285.     no_space();
  286.     strcpy(output_file_name, file_prefix);
  287.     strcpy(output_file_name + len, OUTPUT_SUFFIX);
  288.  
  289.     if (rflag)
  290.     {
  291.     code_file_name = MALLOC(len + 8);
  292.     if (code_file_name == 0)
  293.         no_space();
  294.     strcpy(code_file_name, file_prefix);
  295.     strcpy(code_file_name + len, CODE_SUFFIX);
  296.     }
  297.     else
  298.     code_file_name = output_file_name;
  299.  
  300.     if (dflag)
  301.     {
  302.     defines_file_name = MALLOC(len + 7);
  303.     if (defines_file_name == 0)
  304.         no_space();
  305.     strcpy(defines_file_name, file_prefix);
  306.     strcpy(defines_file_name + len, DEFINES_SUFFIX);
  307.     }
  308.  
  309.     if (vflag)
  310.     {
  311.     verbose_file_name = MALLOC(len + 8);
  312.     if (verbose_file_name == 0)
  313.         no_space();
  314.     strcpy(verbose_file_name, file_prefix);
  315.     strcpy(verbose_file_name + len, VERBOSE_SUFFIX);
  316.     }
  317. }
  318.  
  319.  
  320. open_files()
  321. {
  322.     create_file_names();
  323.  
  324.     if (input_file == 0)
  325.     {
  326.     input_file = fopen(input_file_name, "r");
  327.     if (input_file == 0)
  328.         open_error(input_file_name);
  329.     }
  330.  
  331.     action_file = fopen(action_file_name, "w");
  332.     if (action_file == 0)
  333.     open_error(action_file_name);
  334.  
  335.     text_file = fopen(text_file_name, "w");
  336.     if (text_file == 0)
  337.     open_error(text_file_name);
  338.  
  339.     if (vflag)
  340.     {
  341.     verbose_file = fopen(verbose_file_name, "w");
  342.     if (verbose_file == 0)
  343.         open_error(verbose_file_name);
  344.     }
  345.  
  346.     if (dflag)
  347.     {
  348.     defines_file = fopen(defines_file_name, "w");
  349.     if (defines_file == 0)
  350.         open_error(defines_file_name);
  351.     union_file = fopen(union_file_name, "w");
  352.     if (union_file ==  0)
  353.         open_error(union_file_name);
  354.     }
  355.  
  356.     output_file = fopen(output_file_name, "w");
  357.     if (output_file == 0)
  358.     open_error(output_file_name);
  359.  
  360.     if (rflag)
  361.     {
  362.     code_file = fopen(code_file_name, "w");
  363.     if (code_file == 0)
  364.         open_error(code_file_name);
  365.     }
  366.     else
  367.     code_file = output_file;
  368. }
  369.  
  370.  
  371. int
  372. main(argc, argv)
  373. int argc;
  374. char *argv[];
  375. {
  376.     set_signals();
  377.     getargs(argc, argv);
  378.     open_files();
  379.     reader();
  380.     lr0();
  381.     lalr();
  382.     make_parser();
  383.     verbose();
  384.     output();
  385.     done(0);
  386.     /*NOTREACHED*/
  387. }
  388.